<!--
 * @Author: XieQiu 雪球
 * @Date: 2021-05-10 15:07:49
 * @LastEditTime: 2022-05-14 10:05:59
 * @LastEditors: 血雪球 wash0226@outlook.com
 * @Description: 奖惩积分管理页面 字段未改
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :has-cancel="false"
        :has-save="false"
        :has-edit="false"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">岗位名称:</div>
          <div class="input">
            <el-select
              v-model="postId"
              placeholder="请输入岗位名称"
              clearable
            >
              <el-option v-for="item in stationList" :key="item.postId" :value="item.postId" :label="item.postName" />
            </el-select>
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="88%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="postName" label="岗位名称" />
        <el-table-column prop="firsthandIntegral" label="原始积分" />
        <el-table-column prop="addIntegral" label="奖励增加分数" min-width="100" />
        <el-table-column prop="deductIntegral" label="惩罚扣除分数" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.integralRewardId" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button class="text-btn-cursor cursor-delete" type="text" size="small" @click="handleDelete(scope.row)"><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '奖惩积分'"
        class="formData"
        :visible.sync="dialogFormVisible"
        width="50%"
        top="5vh"
        :before-close="handleClose"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="岗位名称:" prop="postId" class="w50p"><el-select
            v-model="formData.postId"
            placeholder="请选择岗位名称"
            clearable
          >
            <el-option v-for="item in stationList" :key="item.postId" :value="item.postId" :label="item.postName" />
          </el-select>
          </el-form-item>
          <el-form-item label="原始积分:" prop="firsthandIntegral" class="w50p">
            <el-input
              v-model="formData.firsthandIntegral"
              :disabled="!canInput"
              placeholder="请输入原始积分"
              clearable
            />
          </el-form-item>
          <el-form-item label="奖励增加分数:" prop="addIntegral" class="w50p">
            <el-input
              v-model="formData.addIntegral"
              :disabled="!canInput"
              placeholder="请输入奖励增加分数"
              clearable
            />
          </el-form-item>
          <el-form-item label="惩罚扣除分数:" prop="deductIntegral" class="w50p">
            <el-input
              v-model="formData.deductIntegral"
              placeholder="请输入惩罚扣除分数"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">取 消</el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">保 存</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import stationManage from "@/api/basic-info-manage/station-manage/index";
import rewardPointsManagement from "@/api/basic-info-manage/reward-points-management/index";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import SearchBox from "@/components/SearchBox";
import { mymixin } from "@/utils/mixin";
export default {
  name: "",
  components: { ActionBar, Pagination, BaseMainSlot, SearchBox },
  mixins: [mymixin],
  data() {
    return {
      polyedit: false,
      // 设施划分 区域 弹出框
      mapVisible: false,
      // 承租户
      // 地图相关值
      zoom: 18,
      lessee: "",
      // 土地名称
      postId: "",
      // 单位
      stationList: [],
      // 校验规则
      rules: {
        postId: [
          { required: true, message: "请选择岗位名称", trigger: "change" }
        ],
        firstIntegral: [
          { required: true, message: "请输入原始积分", trigger: "blur" }
        ],
        addIntegral: [
          { required: true, message: "请输入奖励增加分数", trigger: "blur" }
        ],
        deductIntegral: [
          { required: true, message: "请输入惩罚扣除分数", trigger: "blur" }
        ]
      }
    };
  },
  watch: {},
  created() {
    // 获取表格数据
    this.getTableDataList();
  },
  mounted() {
    this.getBaseList();
  },
  methods: {
    // 获取表格数据
    getTableDataList() {
      this.formData = {};
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sort: "timestamp",
        entity: {
          postId: this.postId
        }
      };
      rewardPointsManagement.getIntegralRewardList(params).then(res => {
        this.loading = false;
        let data = res.data;
        if (data) {
          this.totalSize = data.totalElements;
          // this.tableData = [];
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    async getBaseList() {
      // 岗位名称
      const { data: resp } = await stationManage.getPostListByCondition();
      this.stationList = resp;
    },
    // handleClose(done) {
    //   this.$confirm("确认关闭？")
    //     .then(_ => {
    //       this.cancel('formData')
    //       done();
    //     })
    //     .catch(_ => {});
    // },
    // 弹出框关闭限制
    handleClose(done) {
      this.cancel('formData')
      done();
      // this.resetActionBar();
    //   this.$confirm("确认关闭？")
    //     .then(_ => {
    //       done();
    //       this.resetActionBar();
    //     })
    //     .catch(_ => {});
    },
    // 初始化操作栏状态
    resetActionBar() {
      // this.$refs["formData"].resetFields();
      this.$refs["tableData"].clearSelection();
      this.canInput = false;
      this.addActive = true;
      this.delActive = false;
      this.saveActive = false;
      this.cancelActive = false;
      this.editActive = false;
    },
    // 搜索
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    // 点击行时
    handleEdit(row) {
      // TODO 点击行元素后 edit
      // this.queryFaciliy();
      this.formData = JSON.parse(JSON.stringify(row));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = false;
      this.canInput = true;
      this.qrActive = true;
      this.dialogFormVisible = true;
    },
    handleSelectionChange(list) {
      this.integralRewardIdList = [];
      this.integralRewardIdList = list.map(item => item.integralRewardId);
      this.delActive = this.integralRewardIdList.length > 0;
    },
    // 添加
    add() {
      this.canInput = true;
      this.nowChanceStau = "add"
      this.titleText = "添加"
      this.dialogFormVisible = true;
      this.addActive = false;
      this.delActive = false;
      this.saveActive = true;
      this.cancelActive = true;
      this.editActive = false;
    },
    delmessage() {
      rewardPointsManagement.delListIntegralRewardInfo(this.integralRewardIdList).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      });
    },
    save(formData) {
      // this.$refs[formData].validate(valid => {
      //   if (valid) {
      if (this.nowChanceStau === "edit") {
        rewardPointsManagement.editIntegralRewardInfo(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg || "修改成功！");
            this.formData = {};
            this.$refs[formData].resetFields();
            this.getTableDataList();
            this.dialogFormVisible = false;
          } else {
            this.$message.warning(res.msg || "修改失败！");
            this.canInput = true
          }
        });
      } else if (this.nowChanceStau === "add") {
        rewardPointsManagement.addIntegralRewardInfo(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg || "新增成功！");
            this.formData = {};
            this.$refs[formData].resetFields();
            this.getTableDataList();
            this.dialogFormVisible = false;
          } else {
            this.$message.warning(res.msg || "新增失败！");
            this.canInput = true
          }
        });
      }
      //   } else {
      //     return false;
      //   }
      // });
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";

::v-deep .el-dialog__wrapper {
  .el-dialog {
    width: 40% !important;
  }
  }

</style>
